close all

%%%%--------load ion data----------------
folderpath = ['/Users/MGHu/Documents/KRb reaction/Data/Figure 1 data'];
filepath = [folderpath,'/ionCountsOverTime.mat'];
load(filepath);
dataIon=ans;
dataTime = dataIon(:,1);        %[ms]
dataIonCount = dataIon(:,2);
Err_IonCount = sqrt(dataIonCount);
xmin = 0;
xmax = 4000;
xdatafit1 = dataTime;
ydatafit1 = dataIonCount;
xdatafit = xdatafit1(xdatafit1<=xmax);
ydatafit = ydatafit1(xdatafit1<=xmax);
xcurve = linspace(min(xdatafit),max(xdatafit),100);
[b, b_Err, ycurve] = twoBodyDecayFit(xdatafit, ydatafit, Err_IonCount, 1, xcurve);
plot(xcurve/1000, ycurve, '-k', 'LineWidth', 1.5);
% string3 = ['t_{1/2} = ',num2str(1/(b(1).*b(2))*1000, '%.3g'), ' \pm', num2str(1/(b(1).*b(2)).*sqrt((b_Err(1)./b(1)).^2 + (b_Err(2)./b(2)).^2), '%.2g'), xunit];
string3 = ['t_{1/2} = ',num2str(1/(b(1).*b(2))*1000, '%.3g'), ' ms'];
text((min(xdatafit) + 0.5*range(xdatafit))/1000, min(ydatafit) + 0.6*range(ydatafit),string3,'Color','k');
hold on;

errorbar(dataTime/1000, dataIonCount, Err_IonCount,...
    's', 'MarkerSize',5,'MarkerEdgeColor',[0.5,0.5,0.5],'MarkerFaceColor','c',...
    'color','k', 'CapSize', 0, 'LineWidth', 0.5);
xlim([0,4]);
ylim([0, 250]);
xlabel('time (s)');
ylabel('KRb^+ (count)');
hold off;
